#include "Effect.h"
#include "SpriteAnimation.h"


Effect::Effect(void)
{
	_listAnimationFrame = NULL;
	_listParticle = NULL;
}


Effect::~Effect(void)
{
	CC_SAFE_RELEASE(_listAnimationFrame);
	CC_SAFE_RELEASE(_listParticle);
}

bool Effect::init()
{
	if (!CCNode::init())
		return false;
	
	setAnimationFrames(CCArray::create());
	setParticles(CCArray::create());

	return true;
}

void Effect::onEnter()
{
	CCNode::onEnter();
	// Start animation
	CCObject* it;
	CCARRAY_FOREACH(_listAnimationFrame, it)
	{
		SpriteAnimation* ani = dynamic_cast<SpriteAnimation*>(it);
		ani->setAnchorPoint(ccp(0.5f, 0.5f));
		//ani->setPosition(ccp(0, 0));
		addChild(ani);
		ani->playAnimation(false);
	}
}

void Effect::onExit()
{
	CCNode::onExit();
	// End animation
	CCObject* it;
	CCARRAY_FOREACH(_listAnimationFrame, it)
	{
		removeChild(dynamic_cast<SpriteAnimation*>(it));
		(dynamic_cast<SpriteAnimation*>(it))->stopAnimation();
	}
}

void Effect::addSpriteAnimation( SpriteAnimation* animation )
{
	if (animation == NULL)
		return;
	_listAnimationFrame->addObject(animation);
	
}

bool Effect::isDone()
{
	if (_listAnimationFrame->count() == 0 && _listParticle->count() == 0)
		return false;
	CCObject* it;
	CCARRAY_FOREACH(_listAnimationFrame, it)
	{
		SpriteAnimation* sa = dynamic_cast<SpriteAnimation*>(it);
		if (!sa->isDone())
			return false;
	}

	return true;
}
